//
//  MovieDetailViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/25.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "LoginViewController.h"
#import "UIImageView+WebCache.h"
@interface MovieDetailViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tittleImg;
@property (weak, nonatomic) IBOutlet UILabel *ratingLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%@", NSHomeDirectory());
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"MovieDetailViewController" owner:self options:nil] lastObject];
    
    // 上方navigationBar的设置
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(movieLogin)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //给各个控件赋值
    [self addText];
    
    
}

/// 给各个控件赋值方法

-(void)addText
{
    [self.tittleImg sd_setImageWithURL:[NSURL URLWithString:_moedel.image]];
    self.dateLabel.text = _moedel.pubdate;
    self.ratingLabel.text = [NSString stringWithFormat:@"评分 : %@", _moedel.rating];
;
    
}

/// 右侧登陆的按钮方法
-(void)movieLogin
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
   BOOL a = [user objectForKey:@"didLogin"];
    if (a) {
        // 显示收藏成功 并且收藏

            // 给我的里面的详情页添加进一个内容 并且确定当前状态为已经收藏
            
            
            NSFileManager *fManage = [NSFileManager defaultManager];
            NSString *MoviePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"movie"];
            [fManage createDirectoryAtPath:MoviePath withIntermediateDirectories:YES attributes:nil error:nil];
           // 创建一个当前登陆的人得文件夹
        NSString *userName = [user objectForKey:@"userName"];
        NSString *userPath = [MoviePath stringByAppendingPathComponent:userName];
        [fManage createDirectoryAtPath:userPath withIntermediateDirectories:YES attributes:nil error:nil];
            
            NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:[userPath stringByAppendingPathComponent:@"movie.plist"]];
            
            if (arr) {
                NSLog(@"33333");

                for (MovieModel *m in arr) {
                    
                    if ([m.title isEqualToString:_moedel.title]) {
                        // 因为包含 所以显示已经收藏过了
                        NSLog(@"1111");
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该电影已经收藏!" preferredStyle:UIAlertControllerStyleAlert];
                        __weak UIAlertController *art = alert;
                        [self presentViewController:alert animated:YES completion:^{
                            [art dismissViewControllerAnimated:YES completion:^{
                                
                            }];
                        }] ;
                        
                        return;
                    }
                }
                // 不包含就把他加入到数组里面
                [arr addObject:_moedel];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
                __weak UIAlertController *art = alert;
                [self presentViewController:alert animated:YES completion:^{
                    [art dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                }] ;
                
            }
            // 如果数组并不存在 就创建一个新的数组 把Model加到数组里面 并显示收藏成功
            else
            {
                NSLog(@"111122222");
                arr = [NSMutableArray array];
                [arr addObject:_moedel];
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
                __weak UIAlertController *art = alert;
                [self presentViewController:alert animated:YES completion:^{
                    [art dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                }] ;
            }
            // 将数组写进本地
            [NSKeyedArchiver archiveRootObject:arr toFile:[userPath stringByAppendingPathComponent:@"movie.plist"]];
            
            
            
//        }
        

        
    }else
    {
    // 进入登陆页面
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    //    loginVC.navigationController.title = @"";
    [self.navigationController presentViewController:navi animated:YES completion:^{
        
    }];
    
    }
    
}
/// 返回上一页
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
